var pageNow = 1;
var pageSize = 5;
var totalCount = 0;
var pageCount = 0;
function queryBorrowList() {
	//radio
	/*if(($("#pageSize").val()=="")||($("#pageSize").val()==null)){
		pageSize=5;
	}else{
		pageSize=$("#pageSize").val();
	}	*/
	var chk_value = [];
	// var chk_value=$("input[name='check']:checked").val();
	$('input[name="check"]:checked').each(function() {
		chk_value.push($(this).val());
	});
	// alert(chk_value);   
	//params 
	var mydata = '{"bookName":"' + $("#bookname").val() + '","readerName":"'
			+ $("#readername").val() + '","pageSize":"' + pageSize
			+ '","pageNow":"' + pageNow + '","chkValue":"' + chk_value + '"}';
	$.ajax({
		type : 'post',
		url : '/bms/borrowreturninfo/borrowlist',// URL
		data : mydata,
		dataType : 'json',
		contentType : "application/json; charset=utf8",
		success : function(data) {
			var list = data.list;
			totalCount = data.totalCount;
			createShowingTable(list);
			if (pageNow == 1) {
				//$("#PPage").hide();
				$("#PPage").attr("disabled", true);
			}
			var pageCount = Math.ceil(totalCount / pageSize);
			if (pageNow == pageCount) {
				//$("#NPage").hide();
				$("#NPage").attr("disabled", true);
			}
			if (pageNow > pageCount) {
				pageNow = 1;
				queryBorrowList();
			}
			$("#page").html(pageNow + "/" + pageCount);
		}
	});
}
function createShowingTable(data) {
	var tStrStart = "<div class='table-responsive'><table class='table table-bordered table-hover'><thead><tr><th>"
			+ "#"
			+ "</th>"
			+ "<th>"
			+ "Book Name"
			+ "</th>"
			+ "<th>"
			+ "Reader Name"
			+ "</th>"
			+ "<th>"
			+ "Borrowed Date"
			+ "</th>"
			+ "<th>"
			+ "Should Return Date"
			+ "</th>"
			+ "<th>"
			+ "Is Return"
			+ "</th>"
			+ "<th>"
			+ "Return Date"
			+ "</th>"
			+ "<th>"
			+ "Over Date"
			+ "</th><th>" + "Return" + "</th>" + "</tr></thead><tbody>";
	$
			.each(
					data,
					function(n, br) {
						var n1 = parseInt(n);
						n1 = n1 + 1;
						var number = (pageNow - 1) * pageSize + n1;
						var brString = "";
						var returnDate = " ";
						var overDate = " ";
						
						if (br.returnDate != null) {
							returnDate = br.returnDate;
						}
						
						if (br.overDate != null) {
							overData = br.overDate;
						}
						
						if (br.isReturn == 0) {
							brString = "<tr><td>"
									+ number
									+ "</td><td>"
									+ br.bookInfo.name
									+ "</td><td>"
									+ br.readerInfo.name
									+ "</td><td>"
									+ br.borrowedDate
									+ "</td><td>"
									+ br.shouldReturnDate
									+ "</td><td>"
									+ br.isReturn
									+ "</td><td>"
									+ returnDate
									+ "</td><td>"
									+ overDate
									+ "</td>"
									+ "<td style='width:70px'><a class='btn mini green' href='/bms/borrowreturninfo/returnconfirm?id="
									+ br.id + "'>Return</a>" + "</td></tr>";
						} else if (br.isReturn == 1) {
							brString = "<tr><td>" + number + "</td><td>"
									+ br.bookInfo.name + "</td><td>"
									+ br.readerInfo.name + "</td><td>"
									+ br.borrowedDate + "</td><td>"
									+ br.shouldReturnDate + "</td><td>"
									+ br.isReturn + "</td><td>" + br.returnDate
									+ "</td><td>" + br.overDate + "</td>"
									+ "<td></td>" + "</tr>";
						}
						tStrStart = tStrStart + brString;
					});
	var tStrEnd = "</tbody></table></div>";
	var bStr =  "<div  style='float:right;margin-right:15px'><input type='button' id='FPage' value='First' class='btn btn-default' onclick='GoToFirstPage()'>"
			+ "<input type='button' id='PPage' value='Pre' class='btn btn-default' onclick='GoToPrePage()'>&emsp;"
			+ "<lable id='page' />&emsp;"
			+ "<input type='button' id='NPage' value='Next' class='btn btn-default' onclick='GoToNextPage()'>"
			+ "<input type='button' id='EPage' value='Last' class='btn btn-default' onclick='GoToEndPage()'></div>";
	tStrStart = tStrStart + tStrEnd + bStr;
	$("#borrowlist").html(tStrStart);
}

function GoToFirstPage() {
	pageNow = 1;
	queryBorrowList();
}

function GoToPrePage() {
	if (pageNow > 1) {
		pageNow -= 1;
		queryBorrowList();
		if (pageNow > 1) {
			$("#PPage").attr("disabled", false);
		} else {
			$("#PPage").attr("disabled", true);
		}
	} else {
		$("#PPage").attr("disabled", true);
	}
}

function GoToNextPage() {
	pageCount = Math.ceil(totalCount / pageSize); //总页数
	if (pageNow < pageCount) {
		pageNow += 1;
		queryBorrowList();
		if (pageNow < pageCount) {
			$("#NPage").attr("disabled", false);
		} else {
			$("#NPage").attr("disabled", true);
		}
	} else {
		$("#NPage").attr("disabled", true);
	}
}

function GoToEndPage() {
	pageCount = Math.ceil(totalCount / pageSize);
	pageNow = pageCount;
	queryBorrowList();
}

function doClearBN(){
	$("#bookname").attr("value","");
	$("#readername").attr("value","");
	$("#cReturn").attr("checked","checked");
	$("#cNoReturn").attr("checked","");
	$("#borrowlist").empty();
}
